package com.xhorse.lockplatform.controller;

import com.xhorse.lockplatform.annotation.RequestLimit;
import com.xhorse.lockplatform.comm.enums.LogModuleEnum;
import com.xhorse.lockplatform.comm.enums.ResultEnum;
import com.xhorse.lockplatform.comm.mvc.AbstractCallbackController;
import com.xhorse.lockplatform.comm.util.LogStatistics;
import com.xhorse.lockplatform.comm.util.RequestUtils;
import com.xhorse.lockplatform.comm.util.StringUtil;
import com.xhorse.lockplatform.service.RegistrantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class RegistrantController extends AbstractCallbackController {

    @Autowired
    private RegistrantService registrantService;

    // 用户注册
    @RequestLimit(count = 1, time = 30000)
    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    @ResponseBody
    public String Registration(HttpServletRequest httpServletRequest, HttpServletResponse response) throws Exception {
        String username = httpServletRequest.getParameter("username");
        String password = httpServletRequest.getParameter("password");
        String ip = RequestUtils.getRemoteIp(httpServletRequest);
        String result = registrantService.Registration(username, password, ip);
        if (result == null) {
            simpleFailed(response, ResultEnum.REGISTER_ERROR_RETRY.getMsg());
        }
        return result;
    }

    // 验证码发送
    @RequestLimit(count = 10, time = 60000)
    @RequestMapping(value = "/sendSMS", method = RequestMethod.POST)
    @ResponseBody
    public void sendSMS(HttpServletResponse response, String username, int type) throws Exception { // type类型 1：注册，2：密码重置
        if (username == null) {
            simpleFailed(response, ResultEnum.REGISTER_INFO_EMPTY.getMsg());
        } else {
            boolean result = false;
            boolean isreg = registrantService.isRegisted(username);
            if ((type == 1 && isreg == false) || (type == 2 && isreg == true)) {
                if (StringUtil.isMobileNO(username)) {
                    result = registrantService.sendSMS(username);
                } else if (StringUtil.isEmail(username)) {
                    result = registrantService.sendEMS(username);
                } else {
                    simpleFailed(response, ResultEnum.REGISTER_USERNAME_ERROR.getMsg());
                }
                if (result == false) {
                    simpleFailed(response, ResultEnum.REGISTER_SMS_SEND_ERROR.getMsg());
                } else {
                    simpleSuccess(response);
                }
            }
            checkComm(response, username, isreg, type);
        }
    }

    /**
     * 公共检测方法
     *
     * @param response
     * @param username
     * @param isreg
     * @param type
     * @throws Exception
     */
    private void checkComm(HttpServletResponse response, String username, boolean isreg, int type) throws Exception {
        if (type == 1 && isreg == true) {
            if (StringUtil.isEmail(username)) {
                simpleFailed(response, ResultEnum.REGISTER_EMAIL_EXIST.getMsg());
            } else {
                simpleFailed(response, ResultEnum.REGISTER_MOBILE_EXIST.getMsg());
            }
        }
        if (type == 2 && isreg == false) {
            simpleFailed(response, ResultEnum.LOGIN_NO_ACCOUNT.getMsg());
        }
    }

    // 验证码校验
    @RequestLimit(count = 10, time = 60000)
    @RequestMapping(value = "/checkSMS", method = RequestMethod.POST)
    @ResponseBody
    public void checkSMS(HttpServletResponse response, String username, String SMS, int type) throws Exception {
        if (username == null || SMS == null) {
            simpleFailed(response, ResultEnum.REGISTER_INFO_EMPTY.getMsg());
        } else {
            boolean isreg = registrantService.isRegisted(username);
            if ((type == 1 && isreg == false) || (type == 2 && isreg == true)) {
                boolean result = registrantService.checkSMSCode(username, SMS);
                if (result == false) {
                    simpleFailed(response, ResultEnum.REGISTER_SMS_MATCH.getMsg());
                } else {
                    simpleSuccess(response);
                }
            }
            checkComm(response, username, isreg, type);
        }
    }

    // 用户登录
    @RequestLimit(count = 5, time = 30000)
    @RequestMapping(value = "/checkLogin.json", method = RequestMethod.POST)
    @ResponseBody
    public String checkLogin(HttpServletRequest request, HttpServletResponse response, String username, String password) throws Exception {
        String result = registrantService.checkLogin(username, password);
        if (result.equals("-2")) {
            simpleFailed(response, ResultEnum.LOGIN_NO_ACCOUNT.getMsg());
        }
        // 记录登录日志
        LogStatistics.log(LogModuleEnum.USER_LOGIN, false, request, result);
        return result;
    }

    // 密码重置
    @RequestLimit(count = 1, time = 30000)
    @RequestMapping(value = "passwordRest", method = RequestMethod.POST)
    @ResponseBody
    public String passwordRest(HttpServletRequest httpServletRequest, HttpServletResponse response) throws Exception {
        String cellPhone = httpServletRequest.getParameter("username");
        String originalPassword = httpServletRequest
                .getParameter("originalPassword");
        String newPassword = httpServletRequest.getParameter("newPassword");
        String result = registrantService.passwordRest(cellPhone,
                originalPassword, newPassword);
        if (ResultEnum.FAILED.getCode().equals(result)) {
            simpleFailed(response, ResultEnum.CRUD_UPDATE_ERROR.getMsg());
        }
        return result;
    }

    // 用户注销
    @RequestLimit(count = 1, time = 30000)
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public String logout(HttpServletRequest request, int customerId) {
        return registrantService.logout(customerId);
    }

    /**
     * 获取默认地址
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "getDefaultAddress", method = RequestMethod.POST)
    @ResponseBody
    public String getDefaultAdress(HttpServletRequest httpServletRequest) {
        return registrantService.getDefaultAdress(httpServletRequest);
    }

    /**
     * 获取地址列表
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "getAddressList", method = RequestMethod.POST)
    @ResponseBody
    public String getAdressList(HttpServletRequest httpServletRequest) {
        return registrantService.getAdressList(httpServletRequest);
    }

    /**
     * 设置默认地址
     *
     * @param httpServletRequest
     * @return
     */
    @RequestLimit(count = 10, time = 10000)
    @RequestMapping(value = "setDefaultAddress", method = RequestMethod.POST)
    @ResponseBody
    public String setDefaultAddress(HttpServletRequest httpServletRequest) {
        return registrantService.setDefaultAddress(httpServletRequest);
    }

    /**
     * 新增地址
     *
     * @param httpServletRequest
     * @return
     */
    @RequestLimit(count = 10, time = 10000)
    @RequestMapping(value = "addAddress", method = RequestMethod.POST)
    @ResponseBody
    public String addAndEditAddress(HttpServletRequest httpServletRequest) {
        return registrantService.addAddress(httpServletRequest);
    }

    /**
     * 编辑地址
     *
     * @param httpServletRequest
     * @return
     */
    @RequestLimit(count = 10, time = 10000)
    @RequestMapping(value = "editAddress", method = RequestMethod.POST)
    @ResponseBody
    public String editAddress(HttpServletRequest httpServletRequest) {
        return registrantService.editAddress(httpServletRequest);
    }


    /**
     * 删除地址
     *
     * @param httpServletRequest
     * @return
     */
    @RequestLimit(count = 10, time = 10000)
    @RequestMapping(value = "deleteAddress", method = RequestMethod.POST)
    @ResponseBody
    public String deleteAddress(HttpServletRequest httpServletRequest) {
        return registrantService.deleteAddress(httpServletRequest);
    }

    /**
     * 获取国家，地区信息列表
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "getAddressTable", method = RequestMethod.POST)
    @ResponseBody
    public String getAddresTable(HttpServletRequest httpServletRequest) {
        return registrantService.getAddresTable(httpServletRequest);

    }

}